/**
 * @author balancejia
 * @description：表单脚本封装，适用于：name='ui-adminlte-form'，要有action和forwardAction属性，操作流程为 列表页->表单页->提交表单->刷新列表
 * @date:2016/8/9
 */

var appform = {};
appform.validateConf = {
	errorElement : 'span',
	errorClass : 'help-block',
	errorPlacement : function(error, element) {
		element.next().remove();
		element.after('<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>');
		element.closest('.form-group').append(error);
	},
	highlight : function(element) {
		$(element).closest('.form-group').addClass('has-error has-feedback');
	},
	success : function(label) {
		var el = label.closest('.form-group').find("input");
		el.next().remove();
		el.after('<span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>');
		label.closest('.form-group').removeClass('has-error').addClass("has-feedback has-success");
		label.remove();
	}
};

appform.validateConf.submitHandler = function(form) {
	var forwardAction = $(form).attr('forward-action');
	var container = $(form).attr('container');
	var options = {
		url : $(form).attr('action'),
		data : $(form).serialize(),
		type : 'post',
		success : function(msg) {
			msg = util.strToObj(msg);
			if (msg.code == "success"){
				dialog.alert(msg.desc);
				if(msg.data){
					$("#appId").val(msg.data.appId)
				}
				$("li[role='presentation'][class='active']").find("a").click();//通过点击标签触发页面内容刷新
				
				$(form).parents('.modal').modal('hide');
				
				if(forwardAction){
					util.ajaxPage(forwardAction, '', false, container);
				}
			}else{
				dialog.alert(msg.desc);
			}
		},
		error : function() {
			dialog.alert("提交异常！");
		}
	};
	$.ajax(options);
};

appform.setValidatorMessage = function() {
	jQuery.extend(jQuery.validator.messages, {
	required : "必填",
	remote : "已存在",
	email : "请输入正确格式的电子邮件",
	url : "请输入合法的网址",
	date : "请输入合法的日期",
	dateISO : "请输入合法的日期 (ISO).",
	number : "请输入合法的数字",
	digits : "只能输入整数",
	creditcard : "请输入合法的信用卡号",
	equalTo : "请再次输入相同的值",
	accept : "请输入拥有合法后缀名的字符串",
	maxlength : jQuery.validator.format("请输入一个 长度最多是 {0} 的字符串"),
	minlength : jQuery.validator.format("请输入一个 长度最少是 {0} 的字符串"),
	rangelength : jQuery.validator.format("请输入 一个长度介于 {0} 和 {1} 之间的字符串"),
	range : jQuery.validator.format("请输入一个介于 {0} 和 {1} 之间的值"),
	max : jQuery.validator.format("请输入一个最大为{0} 的值"),
	min : jQuery.validator.format("请输入一个最小为{0} 的值")
	});
};

appform.init = function(container) {
	appform.setValidatorMessage();
	if(!container){
		container="form[name='ui-adminlte-form']";
	}
	$(container).validate(appform.validateConf);
	// 解决相同name的input只教验第一个bug，要求：name 相同的 input id 必须不同
	if ($.validator) {
		// fix: when several input elements shares the same name, but
		// has different id-ies....
		$.validator.prototype.elements = function() {
			var validator = this , rulesCache = {};
			// select all valid inputs inside the form (no submit or
			// reset buttons)
			// workaround $Query([]).add until
			// http://dev.jquery.com/ticket/2114 is solved
			return $([]).add(this.currentForm.elements).filter(":input").not(":submit, :reset, :image, [disabled]")
					.not(this.settings.ignore).filter(
							function() {
								var elementIdentification = this.id || this.name;
								!elementIdentification && validator.settings.debug && window.console && console.error(
										"%o has no id nor name assigned", this);
								// select only the first element for each name,
								// and only
								// those with rules specified
								if (elementIdentification in rulesCache || !validator.objectLength($(this).rules()))
									return false;
								rulesCache[elementIdentification] = true;
								return true;
							});
		};
	}
};
